int reNormal;
int isAntiAlias;
texture g_txRayCastPos;
sampler2D g_samPos =
sampler_state
{
	Texture = <g_txRayCastPos>;
	AddressU = Clamp;
	AddressV = Clamp;
	MinFilter = Point;
	MagFilter = Linear;
	MipFilter = Linear;
};

texture g_txRayCastNormal;
sampler2D g_samNormal =
sampler_state
{
	Texture = <g_txRayCastNormal>;
	AddressU = Clamp;
	AddressV = Clamp;
	MinFilter = Point;
	MagFilter = Linear;
	MipFilter = Linear;
};

void ShadingFromRayCastingPS( float2 Tex : TEXCOORD0,
							out float4 color : COLOR0,
							out float ZDep: DEPTH0)
{

	float4 Pos = tex2D(g_samPos, Tex);
	if(Pos.w==0.0)				clip(-1);	
	if(Pos.w==0.5)
		if(tex2D(g_samPos, Tex+TexelKernel[0]).w==0||
			tex2D(g_samPos, Tex-TexelKernel[0]).w==0||
			tex2D(g_samPos, Tex+TexelKernel[1]).w==0||
			tex2D(g_samPos, Tex-TexelKernel[1]).w==0)
			clip(-1);

	float3 Norm;
	if(reNormal)
	{
		Norm = tex2D(g_samNormal, Tex);
	}
	else
	{
		//float4 dx = ddx(Pos);
		//float4 dy = ddy(Pos);
		//Norm = cross(dx.xyz, dy.xyz);
		//Norm = normalize(Norm);

		

		float4 dx1, dx2, dy1, dy2;		
		////if(x>0&&x<renPar->renderWidth-1&&y>0&&y<renPar->renderHeight-1)
		//{
			dx1 = Pos-tex2D(g_samPos, Tex-TexelKernel[0]);
			dx2 = tex2D(g_samPos, Tex+TexelKernel[0])-Pos;
			dy1 = Pos-tex2D(g_samPos, Tex-TexelKernel[1]);
			dy2 = tex2D(g_samPos, Tex+TexelKernel[1])-Pos;

		//	//if(x>0&&x<renPar->renderWidth-1&&y>0&&y<renPar->renderHeight-1)
		//	{
				if(abs(dx1.x)+abs(dx1.y)+abs(dx1.z)>abs(dx2.x)+abs(dx2.y)+abs(dx2.z))
				{dx1=dx2;}
				if(abs(dy1.x)+abs(dy1.y)+abs(dy1.z)>abs(dy2.x)+abs(dy2.y)+abs(dy2.z))
				{dy1=dy2;}
		//	}
		//}
		Norm=cross(dx1,dy1);
		Norm = normalize(Norm);

		//color.rgb = Norm; color.a=1.0f; return;
	}
	
	float3 matDiff = ChooseMaterial(Pos, Norm);
	color.rgb = shading(Pos, Norm, matDiff, nLight);
	color.a = 1.0f;

	//Output z depth
	Pos.w = 1.0;
	float4 Pos2 = mul( Pos, g_mWorldViewProjection );
	ZDep = Pos2.z/Pos2.w;
}



technique ShadingFromRayCasting
{
	pass p0
	{
		VertexShader = null;
		PixelShader  = compile ps_3_0 ShadingFromRayCastingPS();
		ZEnable = true;
	}
}

texture g_txNoAntiAlias;
sampler2D g_samNoAntiAlias =
sampler_state
{
	Texture = <g_txNoAntiAlias>;
	AddressU = Clamp;
	AddressV = Clamp;
	MinFilter = Point;
	MagFilter = Linear;
	MipFilter = Linear;
};


void AntiAliasingPS( float2 Tex : TEXCOORD0,
					 float2 vPos: VPOS,
							out float4 color : COLOR0)
{
	float stateFlag = tex2D(g_samPos, Tex).w;
	if(stateFlag==0.0)		clip(-1);
	if(stateFlag==1.0||stateFlag>0)		{color = tex2D(g_samNoAntiAlias, Tex);	return;}

	int isHor;
	if(reNormal)		isHor = 1- tex2D(g_samNormal, Tex).w;
	//if(isHor==1)	{color.r=1.0;color.gba=0;return;}
	//if(isHor==0)	{color.b=1.0;color.rga=0;return;}

	float slope = stateFlag*0.0025;
	//color = slope; return;
	if(slope<0) slope = 1+slope;
	float4 bef = tex2D(g_samNoAntiAlias, Tex-TexelKernel[isHor]);
	float4 aft = tex2D(g_samNoAntiAlias, Tex+TexelKernel[isHor]);
	color = bef*slope+aft*(1-slope);
	

	
	//color = abs(Pos.w)*0.25;
}



technique AntiAliasing
{
	pass p0
	{
		VertexShader = null;
		PixelShader = compile ps_3_0 AntiAliasingPS();
	}
}
